import Api from '../constants/Api';
/**
 * 函数防抖
 * @param {Function} fn 需要防抖处理的函数
 * @param {Number} wait 等待时间 1000 = 1s
 */
export function debounce(fn, wait) {
  let timerId = null
  let flag = true
  return function () {
    clearTimeout(timerId)
    if (flag) {
      fn.apply(this, arguments)
      flag = false
    }
    timerId = setTimeout(() => {
      flag = true
    }, wait)
  }
}

/**
 * 对象合并
 * @param {Object} target 合并后的目标对象
 */
export function merge(target) {
  for (let i = 1, j = arguments.length; i < j; i++) {
    let source = arguments[i] || {}
    for (const prop in source) {
      if (source.hasOwnProperty(prop)) {
        let value = source[prop]
        if (value !== undefined) {
          target[prop] = value
        }
      }
    }
  }
  return target
}


// 下拉列表数据查询
export function getSelectData(that, data, name, callback) {
  that.$axios.post(Api.selectPkInfo,
    {
      sysHead: {
        serviceCode: 'MbsdAccount',
        messageType: '',
        messageCode: ''
      },
      body: {
        pkInfo: data
      }
    }
  ).then((res) => {
    that[name] = res.data
    // 回调函数，当函数执行成功后调用函数callback,callback为可选参数
    callback && (typeof callback === 'function') && callback()
  })
}

